package oj;

public class LC4 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int n=nums1.length;
        int m=nums2.length;
        int[] arr=new int[n+m];
        if(n==0){
            if(m%2==0){
                return (nums2[m/2-1]+nums2[m/2])/2.0;
            }else{
                return nums2[m/2];
            }
        }
        if(m==0){
            if(n%2==0){
                return (nums1[n/2-1]+nums1[n/2])/2.0;
            }else{
                return nums1[n/2];
            }
        }
        int count=0;
        int i=0,j=0;
        while(count!=n+m){
            if(i==n){
                while(j!=m){
                    arr[count++]=nums2[j++];
                }
                break;
            }
            if(j==m){
                while(i!=n){
                    arr[count++]=nums1[i++];
                }
                break;
            }
            if(nums1[i]<nums2[j]){
                arr[count++]=nums1[i++];
            }else if(nums1[i]>=nums2[j]){
                arr[count++]=nums2[j++];
            }
        }
        if(count%2==0){
            return (arr[count/2-1]+arr[count/2])/2.0;
        }else{
            return arr[count/2];
        }
    }
}
